package com.glodon.paas.account.service.util;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.glodon.paas.account.api.bean.AccountConstant;
import com.glodon.paas.account.api.bean.Address;
import com.glodon.paas.account.api.bean.Company;
import com.glodon.paas.account.api.bean.Contact;
import com.glodon.paas.account.api.bean.User;
import com.glodon.paas.account.biz.UserBiz;
import com.glodon.paas.account.dao.domain.CompanyEntity;
import com.glodon.paas.account.dao.domain.ContactEntity;
import com.glodon.paas.account.dao.domain.UserEntity;
import com.glodon.paas.exception.PaasServerExceptionFactory;
import com.glodon.paas.exception.i18n.ErrorCode;
import com.glodon.paas.util.BeanFactoryUtil;
import com.glodon.paas.util.SimpleBeanConverter;
import com.glodon.paas.util.StringUtil;

public class AccountUtil {
	//account.userName可以使用的字符集
	private static Set<Integer> accountCharSet = new HashSet<Integer>();
	static{
		String temp="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.";
		for(int i=0; i<temp.length(); i++){
			accountCharSet.add((int) temp.charAt(i));
		}
	}
	/**
	 * 判断账号类型,同时检查账号合法性
	 * 
	 * @param accountName
	 * @param badException	无效账号名是否抛异常
	 * @return
	 */
	public static Integer judgeAccountName(String accountName, boolean badException){
		if(StringUtil.isEmail(accountName)){
			return AccountConstant.ACCOUNT_NAME_EMAIL;
		}
		
		if(StringUtil.isMobile(accountName)){
			return AccountConstant.ACCOUNT_NAME_MOBILE;
		}
		
//		if(isAccountName(accountName)){
//			return AccountConstant.ACCOUNT_NAME_NOMAL;
//		}
		
		if(badException){
			throw PaasServerExceptionFactory.validateException("invilide accountName:"+accountName, null).setErrorCode(ErrorCode.ACCOUNT_NAME_BAD);
		}
		return AccountConstant.ACCOUNT_NAME_BAD;
	}
	
	/**
	 * 普通用户名校验
	 * length >= 4
	 * not mobile	不能纯数字
	 * not email	不能含有@
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isAccountName(String str){
		if(str == null || str.length() < 4) {
			return false;
		}
		if (StringUtil.isMobile(str)) {
			return false;
		}
		if (StringUtil.isEmail(str)) {
			return false;
		}
		// 验证字符合法性
		boolean contains = false;
		for(int i=0; i<str.length(); i++){
			contains = accountCharSet.contains((int) str.charAt(i));
			if(!contains){
				return false;
			}
		}
		return true;
	}
	
	public static void cascadeUser(UserEntity dbuser, User user){
		UserBiz userBiz = (UserBiz)BeanFactoryUtil.getBean("userBiz");
		if(StringUtils.isNotEmpty(dbuser.getAddressId())){
			user.setAddress(SimpleBeanConverter.converte(userBiz.getAddress(dbuser.getAddressId()), Address.class));
		}
		if(StringUtils.isNotEmpty(dbuser.getContactId())){
			ContactEntity dbcontact = userBiz.getContact(dbuser.getContactId());
			Contact contact = SimpleBeanConverter.converte(dbcontact, Contact.class);
			if(StringUtils.isNotEmpty(dbcontact.getAddressId())){
				contact.setAddress(SimpleBeanConverter.converte(userBiz.getAddress(dbcontact.getAddressId()), Address.class));
			}
			user.setContact(contact);
		}
		if(StringUtils.isNotEmpty(dbuser.getCompanyId())){
			CompanyEntity dbcompany = userBiz.getCompany(dbuser.getCompanyId());
			Company company = SimpleBeanConverter.converte(dbcompany, Company.class);
			if(StringUtils.isNotEmpty(dbcompany.getAddressId())){
				company.setAddress(SimpleBeanConverter.converte(userBiz.getAddress(dbcompany.getAddressId()), Address.class));
			}
			user.setCompany(company);
		}
}
}
